package com.csjy.migration.hnnd;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class ForbiddenPosition {

  private DbUtil sourceDbUtil;
  public ForbiddenPosition(DbUtil sourceDbUtil) {
    this.sourceDbUtil = sourceDbUtil;
  }

  public void start(ProgramArgument programArgument) throws SQLException {
    String[] orgCodeArr = programArgument.getOrgCodes().split(",");
    for(int i =0;i<orgCodeArr.length;i++) {
      String orgCode = orgCodeArr[i];
      if(orgCode.equals("R")){
        continue;
      }
      this.addForbiddenPosition(orgCode);
    }
  }

  public void addForbiddenPosition(String parentOrgId) throws SQLException {
    // 判断机构下是否有 禁止新业务 岗位 （根据名称)，没有，则新建，有则查询到对应记录 ，最终得到 禁止新业务orgId
    String forbiddenPositionOrgId = null;

    String findForbiddenPositionOrg = "\n" +
        "SELECT ORGID FROM jwframe.FT_ORGNIZATION\n" +
        "WHERE PARENTORGID = ? AND ORGTYPEID  = 1 AND ORGNAME = '禁止新业务' ";
    List<Object> forbiddenOrgIdList = sourceDbUtil.selectSingleColumn(findForbiddenPositionOrg, Arrays.asList(parentOrgId));
    if(forbiddenOrgIdList.size() > 0) {
      forbiddenPositionOrgId = forbiddenOrgIdList.get(0).toString();
    }

    if(forbiddenPositionOrgId != null) {
      // 查询机构下其他岗位的人员id
      String findOtherPositionEmployeeSql = "\n" +
          "SELECT \n" +
          "\tOPERATORID   \n" +
          "FROM ft_EmpRefOrg \n" +
          "WHERE  orgID IN (\n" +
          "\tSELECT \n" +
          "\t\torgID\n" +
          "\tFROM jwframe.ft_orgnization \n" +
          "\tWHERE parentOrgID = ? AND orgTypeID = 1 AND ORGID  != ?\n" +
          ")\n" +
          "GROUP BY OPERATORID \n";
      List<Object> otherPositionEmployeeIdList = sourceDbUtil.selectSingleColumn(findOtherPositionEmployeeSql, Arrays.asList(parentOrgId, forbiddenPositionOrgId));

      // 插入其他岗位所有人员到 禁止新业务 岗位中
      for(Object obj : otherPositionEmployeeIdList) {
        String queryFtEmpRefOrgSql = "SELECT OPERATORID FROM FT_EMPREFORG WHERE OPERATORID = ? AND ORGID = ?";
        List<Object> ftEmpRefOrgList = sourceDbUtil.selectSingleColumn(queryFtEmpRefOrgSql, Arrays.asList(obj.toString(),forbiddenPositionOrgId));
        if(ftEmpRefOrgList.size() > 0) {
          continue;
        }
        String insertFtEmpRefOrgSql = "INSERT INTO FT_EMPREFORG\n" +
            "(ORGCODE, EMPCODE, OPERATORID, ORGID, POSITIONID, CREATE_TIME, MODIFY_TIME, NODE_NO, MODIFY_NODE_NO)\n" +
            "VALUES(NULL, NULL, ?, ?, NULL, TIMESTAMP '2012-06-19 16:10:03.000000', TIMESTAMP '2012-06-19 16:10:03.000000', NULL, NULL)";
        sourceDbUtil.updateByParams(insertFtEmpRefOrgSql, Arrays.asList(obj.toString(),forbiddenPositionOrgId),false);
      }
    }

    // 判断是否还有子部门，若有进行递归处理
    String findChildrenOrgSql = "select ORGID from ft_orgnization where parentorgid = ? and orgtypeid = 5";
    List<Map> childrenOrgList = sourceDbUtil.selectByParams(findChildrenOrgSql, Arrays.asList(parentOrgId));
    for(Map childrenOrg : childrenOrgList) {
      addForbiddenPosition(childrenOrg.get("ORGID").toString());
    }
  }
}
